//
//  LZStaffAccountListViewController.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/1.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZStaffAccountListViewController.h"
#import "LZCreatStaffMemberViewController.h"
#import "UIViewController+NavigationItemSetting.h"
#import "LZStaffManagerListCell.h"
#import "LZStaffModel.h"
#import "LZManagerStaffPresenter.h"
#import "LZAlertView.h"
#import "extobjc.h"

@interface LZStaffAccountListViewController ()<UITableViewDataSource,UITableViewDelegate,LZStaffManagerListCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UILabel *staffCountLabel;
@property (strong, nonatomic) IBOutlet UIView *footView;
@property (weak, nonatomic) IBOutlet UILabel *footViewNoDataLabel;
@property (strong, nonatomic) LZManagerStaffPresenter *staffPresenter;
@property (strong, nonatomic) NSMutableArray *dataArr;

@end

@implementation LZStaffAccountListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configureUI];
    
    [self configureNavBar];
    
    [self configureData];
}

- (void)configureUI
{
    self.umLogPageViewName = @"员工管理列表";
    self.title = LZLocalizedString(@"manage_staff", nil);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = self.topView;
    self.tableView.tableFooterView = self.footView;
    self.footViewNoDataLabel.text = LZLocalizedString(@"refresh_footer_no_data", nil);
    self.footViewNoDataLabel.hidden = YES;
    [self.tableView registerNib:[UINib nibWithNibName:@"LZStaffManagerListCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

- (void)configureData
{
    if (!_staffPresenter) {
        _staffPresenter = [[LZManagerStaffPresenter alloc] init];
    }
        
    @weakify(self);
    [self.staffPresenter getStaffListWithTimeStamp:0 PageNum:1 Limit:100 Success:^(NSArray *listArr, NSInteger totalCount) {
        @strongify(self);
        self.dataArr = listArr.mutableCopy;
        self.staffCountLabel.text = [NSString stringWithFormat:@"%ld",totalCount];
        if (self.dataArr.count == 0) {
            self.footViewNoDataLabel.hidden = NO;
        }else{
            self.footViewNoDataLabel.hidden = YES;
        }
        [self.tableView reloadData];
        
    } Failure:^(NSString *fail) {
        
    }];

}

- (void)configureNavBar
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 40, 40);
    UIImage *addImage = [UIImage imageNamed:@"staff_add"];
    [btn setImage:addImage forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(creatNewStaffAction) forControlEvents:UIControlEventTouchUpInside];
    [self navigationItemSetting:@[btn] type:NAVIGATIONITEMSETTING_RIGHT];
}

#pragma mark -
#pragma mark private event
- (void)creatNewStaffAction
{
    LZCreatStaffMemberViewController *vc = [[LZCreatStaffMemberViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    vc.EditOrAddStaffSuccessBlock = ^(){
        [self configureData];
    };
}

#pragma mark -
#pragma mark tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  113;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LZStaffManagerListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.staffCellDelegate = self;
    LZStaffModel *m = self.dataArr[indexPath.row];
    [cell loadCellContent:m indexPath:indexPath];
    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView: (UITableView *)tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}


- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath //last ios 8
{
    @weakify(self);
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"      " handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"staff_delete_staff", nil) andImage:nil cancelButtonTitle:LZLocalizedString(@"cancel", nil) otherButtonTitles:LZLocalizedString(@"confirm", nil),nil];
        [alert show];
        alert.clickButtonIndexBlock = ^(NSInteger index){
            @strongify(self);
            if (index == 1) {
                [self startLoadingViewForView:self.view loadingViewUserInteractionEnabled:YES];
                LZStaffModel *m = self.dataArr[indexPath.row];
                [self.staffPresenter deleteStaffWithStaffID:m.staffId Success:^(id data) {
                    [self stopLoadingViewForView:self.view];
                    [self.dataArr removeObjectAtIndex:indexPath.row];
                    //发现删除动画不可调控. 这两句代码是防止删除动画时,删除按钮往上窜的效果.
                    LZStaffManagerListCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
                    cell.deleteImgView.hidden = YES;
                    
                    [self.tableView beginUpdates];
                    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                    [self.tableView endUpdates];
                    
                    int totalCount = [self.staffCountLabel.text intValue];
                    if (totalCount > 1) {
                        totalCount --;
                    }else{
                        totalCount = 0;
                        self.footViewNoDataLabel.hidden = NO;
                    }
                    self.staffCountLabel.text = [NSString stringWithFormat:@"%d",totalCount];
                } Failure:^(NSString *fail) {
                    [self stopLoadingViewForView:self.view];
                }];

            }
        };
    }];
    
    rowAction.backgroundColor = [UIColor clearColor];

    return @[rowAction];
}


#pragma mark -
#pragma mark cell delegate
- (void)editStaffWithIndex:(NSIndexPath *)indexP
{
    DLog(@"edit staff");
    LZCreatStaffMemberViewController *vc = [[LZCreatStaffMemberViewController alloc] init];
    LZStaffModel *m = self.dataArr[indexP.row];
    LZStaffModel *mTmep = [[LZStaffModel alloc] init];
    mTmep.staffId = m.staffId;
    mTmep.staffName = m.staffName;
    mTmep.staffAccounts = m.staffAccounts;
    mTmep.password = m.password;
    mTmep.role = m.role;
    
    vc.staffModel = mTmep;
    vc.isEditStaff = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    vc.EditOrAddStaffSuccessBlock = ^(){
        [self configureData];
    };
}

@end
